package io.zhengsh.nio.buffer;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class BufferDemo2 {
    public static void main(String[] args) {

    }

    // 缓冲区分片
    @Test
    public void b01() {
        ByteBuffer buffer = ByteBuffer.allocate(10);
        // 放入数据
        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);
        }

        // 创建子缓冲区
        buffer.position(3);
        buffer.limit(7);
        ByteBuffer slice = buffer.slice();

        // 改变子缓冲区中的内容
        for (int i = 0; i < slice.capacity(); i++) {
            byte b = slice.get(i);
            b *= 10;
            slice.put(i, b);
        }

        // 复位
        buffer.position(0);
        buffer.limit(buffer.capacity());

        while (buffer.remaining() > 0) {
            System.out.println(buffer.get());
        }
    }

    // 只读缓冲区
    @Test
    public void b02() {
        ByteBuffer buffer = ByteBuffer.allocate(10);

        // 放入数据
        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);
        }

        // 创建一个只读缓冲区
        ByteBuffer readOnlyBuf = buffer.asReadOnlyBuffer();
        for (int i = 0; i < buffer.capacity(); i++) {
            byte b = buffer.get(i);
            b *= 10;
            buffer.put(i, b);
        }

        readOnlyBuf.position(0);
        readOnlyBuf.limit(readOnlyBuf.capacity());

        while (readOnlyBuf.remaining() > 0) {
            System.out.println(readOnlyBuf.get());
        }
    }

    // 直接缓冲区，文件拷贝
    @Test
    public void b03() throws IOException {
        String filePath = "/Users/zhengsh/sourcecode.io/zhengsh-vvip/nio/src/main/resources/01.txt";
        FileInputStream inputStream = new FileInputStream(filePath);
        FileChannel fileInChannel = inputStream.getChannel();

        String outPath = "/Users/zhengsh/sourcecode.io/zhengsh-vvip/nio/src/main/resources/02.txt";
        FileOutputStream outputStream = new FileOutputStream(outPath);
        FileChannel fileOutChannel1 = outputStream.getChannel();

        //  使用 allocateDirect , 而不是 allocate
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);

        while (true) {
            buffer.clear();
            int r = fileInChannel.read(buffer);
            if (r == -1) {
                break;
            }
            buffer.flip();
            fileOutChannel1.write(buffer);
        }
        fileInChannel.close();
        fileOutChannel1.close();
    }

    @Test
    public void b04() throws IOException {
        String filePath = "/Users/zhengsh/sourcecode.io/zhengsh-vvip/nio/src/main/resources/01.txt";
        RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
        mappedByteBuffer.put(0, (byte) 97);
        mappedByteBuffer.put(1023, (byte) 122);
        fileChannel.close();
    }
}
